package cn.edu.zut.internship.service.impl.notify;

import cn.edu.zut.internship.bean.sys.Result;
import cn.edu.zut.internship.iservice.notify.NoticeSV;
import cn.edu.zut.internship.mapper.notify.NoticeMapper;
import cn.edu.zut.internship.utils.PrimaryKeyUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class NoticeSVImpl implements NoticeSV {
    @Autowired
    NoticeMapper noticeMapper;
    @Autowired
    PrimaryKeyUtil primaryKeyUtil;
    @Override
    public Result getNoticeList(Map params) {
        Result result = new Result();
        List<Map> noticeList = noticeMapper.getNoticeList(params);
        int total = noticeMapper.getNoticeListCount(params);
        for(Map map:noticeList){
            int targetType = (int) map.get("targetType");
            if(targetType==0){
                map.put("targetType","公开");
            }else if(targetType==1){
                map.put("targetType","用户");
            }else if(targetType==2){
                map.put("targetType","学生");
            }else if(targetType==3){
                map.put("targetType","教师");
            }
        }
        result.setBeans(noticeList);
        result.getBean().put("total",total);
        return result;
    }

    @Override
    public Result addNotice(Map params) {
        Result result = new Result();
        Long noticeId = primaryKeyUtil.generatePrimaryKey("notice");
        params.put("noticeId",noticeId);
        noticeMapper.addNotice(params);
        return result;
    }

    @Override
    public Result getNotice(Map params) {
        Result result = new Result();
        Map notice = noticeMapper.getNotice(params);
        result.setBean(notice);
        return result;
    }

    @Override
    public Result deleteNotice(Map params) {
        Result result = new Result();
        noticeMapper.deleteNotice(params);
        return result;
    }

    @Override
    public Result updateNotice(Map params) {
        Result result = new Result();
        noticeMapper.updateNotice(params);
        return result;
    }

    @Override
    public Result getMyNoticeList(Map params) {
        Result result = new Result();
        int userType = (int) params.get("userType");
        List<Integer> userTypeList = new ArrayList();
        if(userType == 0){
            userTypeList.add(0);
            userTypeList.add(1);
            userTypeList.add(2);
            userTypeList.add(3);
        }else if(userType == 1) {
            userTypeList.add(0);
            userTypeList.add(1);
            userTypeList.add(3);
        }else if(userType == 2){
            userTypeList.add(0);
            userTypeList.add(1);
            userTypeList.add(2);
        }
        params.put("userTypeList",userTypeList);
        List<Map> noticeList = noticeMapper.getMyNoticeList(params);
        int total = noticeMapper.getMyNoticeListCount(params);
        result.setBeans(noticeList);
        result.getBean().put("total",total);
        return result;
    }
}
